﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProjectStore30032013.DAL;

namespace ProjectStore30032013.PRESENTATION
{
    public partial class frmExportBefore : Form
    {
        OrderBefore orderBefore = new OrderBefore(); 
        List<Order_Detail> lstOrder = new List<Order_Detail>();



        public frmExportBefore()
        {
            InitializeComponent();
        }
        

        private void frmExportBefore_Load(object sender, EventArgs e)
        {
            txtOrderID.Text = new Random().Next(1, 100).ToString();
            DataCSQLDataContext datacontext = new DataCSQLDataContext();
            try
            {
                IEnumerable<Product> lstProduct = from d in datacontext.Products
                                                  select d;
                MessageBox.Show(lstProduct.Count().ToString());
                foreach (Product p in lstProduct) cmbProductID.Items.Add(p);
                cmbProductID.DisplayMember = "ProductID";
                cmbProductID.ValueMember = "ProductID";
                cmbProductID.SelectedIndex = 0;
            }
            catch
            {
                //khong co du lieu
            }
            try
            {
                cmbCustomerID.DataSource = from d in datacontext.Customers
                                           select d;
                cmbCustomerID.DisplayMember = "CustomerID";
            }
            catch
            {
                //khong co du lieu
            }
            try
            {
                //cmbUnitMoney.DataSource = from d in datacontext.UnitMoneys
                //                          select d;
                //cmbUnitMoney.DisplayMember = "Id";
            }
            catch
            {
                //khong co lieu
            }
        }


        //Load data to all control
        private void LoadDataToAllControl()
        {
            txtOrderID.Enabled = false;
            this.dtpDateOrder.Value = Controler.timeOfProgram();
            this.dtpDateShip.Value = Controler.timeOfProgram();


        }

        private void btnProductAdd_Click(object sender, EventArgs e)
        {
          

            if (cmbProductID.Items.Count == 0) MessageBox.Show("Không có sản phẩm nào cả!");
            else if (cmbProductID.SelectedIndex == -1) MessageBox.Show("Chưa chọn sản phẩm!");
            else if (string.IsNullOrWhiteSpace(txtProductNumber.Text)) MessageBox.Show("Chưa nhập số lượng!");
                else
                {
                    Product productSelected = (Product)cmbProductID.SelectedItem;
                    Order_Detail orderDetail = new Order_Detail();
                    orderDetail.OrderID = txtOrderID.Text;
                    orderDetail.ProductID = cmbProductID.Text;
                    orderDetail.Quantity = Convert.ToInt16(txtProductNumber.Text);
                    orderDetail.UnitPrice = productSelected.Price.Value;
                    lstOrder.Add(orderDetail);

                    decimal totalMoney = productSelected.Price.Value * Convert.ToInt32(txtProductNumber.Text) * (1 + (decimal)productSelected.RateVAT.Value / 100);
                    dgvOrderInfomation.Rows.Add(productSelected.ProductName, txtProductNumber.Text, productSelected.Price.ToString(), productSelected.RateVAT.Value, totalMoney);

                    //remover from combobox
                    cmbProductID.Items.Remove(cmbProductID.SelectedItem);
                    cmbProductID.Text = "";
                }
        }

        private void btnExportOrder_Click(object sender, EventArgs e)
        {
            try
            {
                InsertDatabase();
                MessageBox.Show("Đã thêm hóa đơn xong!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi" + ex.Message);
            }
        }


        private void TestDataInput()
        {
            //kiem tra du lieu nhap vao 
            if (txtOrderID.Text == "") MessageBox.Show("Chưa có mã hóa đơn!");
            //else if (dtpDateOrder.Value-Control.) MessageBox.Show("Thời gian không hợp lệ!");
            else if (dtpDateShip.Value < dtpDateOrder.Value) MessageBox.Show("Thời gian giao hàng không hợp lệ!");
            else if (cmbCustomerID.SelectedIndex == -1) MessageBox.Show("Bạn chưa chọn khách hàng");
            else if (cmbEmployee.SelectedIndex == -1) MessageBox.Show("chưa chọn nhân viên");
            else if (cmbProductID.SelectedIndex == -1) MessageBox.Show("chưa chọn sản phẩm");
            else if (cmbUnitMoney.SelectedIndex == -1) MessageBox.Show("Chưa chọn loại tiền");
            
        }


        private void InsertDatabase ()
        {
           
                //đưa dữ liệu vào database
                DataCSQLDataContext dataContext = new DataCSQLDataContext(); //creat a before
                OrderBefore newOrder = new OrderBefore();
                newOrder.OrderBeforeId = txtOrderID.Text;
                newOrder.CustomerId = ((Customer)cmbCustomerID.SelectedValue).CustomerID;
                newOrder.EmployId = ((Employee)cmbEmployee.SelectedValue).EmployeeID.ToString();
                newOrder.MoneyDeposit = Convert.ToDecimal(txtMoneyDeposit.Text);
                newOrder.OrderBeforeDate = dtpDateOrder.Value;
                newOrder.RequirementDate = dtpDateShip.Value;
                dataContext.OrderBefores.InsertOnSubmit(newOrder);
                foreach (Order_Detail orderDetail in lstOrder)
                    dataContext.Order_Details.InsertAllOnSubmit(lstOrder);
                dataContext.SubmitChanges();
        }
        


     
        private void cmbProductID_SelectedIndexChanged(object sender, EventArgs e)
        {
            Product productSelected = (Product)cmbProductID.SelectedItem;
            txtProductName.Text = productSelected.ProductName.ToString();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbCustomerID_SelectedIndexChanged(object sender, EventArgs e)
        {
            Customer customerSelected = (Customer)cmbCustomerID.SelectedItem;
            txtCustomerName.Text = customerSelected.ContactName.ToString();
        }
    }
}
